package com.asiainfo.bms.controller;


import com.asiainfo.bms.model.Employee;
import com.asiainfo.bms.service.EmployeeService;
import com.asiainfo.bms.util.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author liu.hx
 * @since 2021-07-13
 */
@CrossOrigin
@Api(tags = "员工管理")
@RestController
@RequestMapping("/bms/employees")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("获取所有员工")
    @GetMapping("/")
    public R queryAll() {
        List<Employee> list = employeeService.list();
        return list != null && !list.isEmpty() ?
                R.ok("获取所有员工成功").setData("items", list) : R.error("获取所有员工失败");
    }

    @ApiOperation("分页获取员工")
    @GetMapping("/{page}/{limit}")
    public R queryByPage(@ApiParam(value = "当前页码", required = true)
                         @PathVariable Long page,
                         @ApiParam(value = "每页记录数", required = true)
                         @PathVariable Long limit,
                         @ApiParam("员工查询对象") Employee employee) {
        IPage<Employee> pageParam = employeeService.pageSelect(page, limit, employee);
        List<Employee> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return list != null ?
                R.ok("分页获取员工成功").setData("items", list).setData("total", total) : R.error("分页获取员工失败");
    }

    @ApiOperation("修改一个员工")
    @PutMapping("/")
    public R update(@ApiParam("员工对象")
                    @RequestBody Employee employee) {
        return employeeService.updateById(employee) ?
                R.ok("修改员工成功") : R.error("修改员工失败");
    }

    @ApiOperation("新增一个员工")
    @PostMapping("/")
    public R insert(@ApiParam(value = "员工对象", required = true)
                    @RequestBody Employee employee) {
        return employeeService.save(employee) ?
                R.ok("新增员工成功") : R.error("新增员工失败");
    }

    @ApiOperation("根据ID获取一个员工")
    @GetMapping("/{id}")
    public R queryById(@ApiParam(value = "员工ID", required = true)
                       @PathVariable String id) {
        Employee employee = employeeService.getById(id);
        return employee != null ?
                R.ok("获取ID=" + id + "的员工成功").setData("item", employee) : R.error("获取ID=" + id + "的员工失败");
    }

    @ApiOperation("根据工号获取一个员工(判断员工是否存在)")
    @GetMapping("/exist/{no}")
    public R queryByNo(@ApiParam(value = "员工号", required = true)
                       @PathVariable String no) {
        Employee employee = employeeService.getByNo(no);
        return employee != null ?
                R.ok("获取工号=" + no + "的员工成功").setData("item", employee) : R.ok("获取工号=" + no + "的员工失败");
    }

}

